package com.ymatou.app.rec.testcase;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.app.rec.parameter.SearchSellerBean;
import com.ymatou.app.rec.service.SearchSellerCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
/**
*店铺和卖家搜索接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SearchSeller{
	private static SearchSellerBean searchsellerBean;
	private static SearchSellerCall searchsellerCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("店铺和卖家搜索接口");
	}

	@Before
	public void caseUp() {
		searchsellerBean=new SearchSellerBean();
		searchsellerCall=new SearchSellerCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(searchsellerCall.getOpurl());
		//Logger.generateResult();
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SearchSeller_001() {
		Logger.start(true,"搜索店铺接口返回状态");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			searchsellerBean.setPlatform("pc");//pc店铺搜索
			searchsellerBean.setQuery("keyword(nickname,1)");//店铺检索关键字
			searchsellerBean.setFl("iuserid,shopname,nickname,originnickname,ismerchant");
			searchsellerBean.setLimit("0,10");
			searchsellerCall.setData(searchsellerBean);
			searchsellerCall.callService();			
			Logger.comment("searchsellerCall send:" + searchsellerCall.getEntityString());
			Logger.comment("searchsellerCall response:" + searchsellerCall.getReturnData());
			Logger.verifyEquals("OK",searchsellerCall.getSuccess(), "接口调用是否成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SearchSeller_002() {
		Logger.start(true,"接口返回内容是否匹配关键字");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			searchsellerBean.setPlatform("appbuyer");//app卖家搜索
			String keyword = "测试";
			searchsellerBean.setQuery("keyword(nickname,"+keyword+")");//卖家检索关键字
			searchsellerBean.setFl("iuserid,shopname,nickname,originnickname,ismerchant");
			searchsellerBean.setLimit("0,10");
			searchsellerCall.setData(searchsellerBean);
			searchsellerCall.callService();	
			Logger.comment("searchsellerCall send:" + searchsellerCall.getEntityString());
			Logger.comment("searchsellerCall response:" + searchsellerCall.getReturnData());
			
			//获取接口返回数据
			JSONArray sellList = searchsellerCall.getSellInfo();
			//定义一个数组存放originnickname值
			String[] nickAry =new String[sellList.length()]; 
		boolean y=false;
			//Logger.debug(searchsellerCall.getSellInfo());
			for(int i=0;i<sellList.length();i++){
				
				JSONObject sellinfo =  (JSONObject) sellList.get(i);
				nickAry[i] = (String) sellinfo.get("originnickname");
				//Logger.debug(nickAry[i].contains(keyword));//如果存在返回true，不存在返回false
				if(nickAry[i].contains(keyword)==false){
					Logger.verifyEquals(true,nickAry[i].contains(keyword), "返回结果中存在不包含关键字的结果");
					break;
				}else{
					y=true;
				}
				
			}
			
			Logger.verifyEquals(true,y, "返回结果包含关键字的结果");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SearchSeller_003() {
		Logger.start(true,"接口返回内容是否区分商户和卖家字段");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			searchsellerBean.setPlatform("appbuyer");//app卖家搜索
			String keyword = "测试";
			searchsellerBean.setQuery("keyword(nickname,"+keyword+")");//卖家检索关键字
			searchsellerBean.setFl("iuserid,shopname,nickname,originnickname,ismerchant");
			searchsellerBean.setLimit("0,10");
			searchsellerCall.setData(searchsellerBean);
			searchsellerCall.callService();	
			Logger.comment("searchsellerCall send:" + searchsellerCall.getEntityString());
			Logger.comment("searchsellerCall response:" + searchsellerCall.getReturnData());
			
			//获取接口返回数据
			JSONArray sellList = searchsellerCall.getSellInfo();
			boolean y=false; 
			for(int i=0;i<sellList.length();i++){
				
				JSONObject sellinfo =  (JSONObject) sellList.get(i);
				boolean contains = sellinfo.has("ismerchant");//检查是否有返回ismerchant值
				if(contains == false){
					Logger.verifyEquals("true",contains, "返回结果中没有ismerchant字段值");
					break;
				}else{
					y=true;
				}
				
				
			}
			Logger.verifyEquals(true,y, "返回结果中有ismerchant字段值");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}